package com.ttg.utils.connection;

//~--- non-JDK imports --------------------------------------------------------

import org.apache.http.conn.HttpClientConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

//~--- JDK imports ------------------------------------------------------------

//~--- classes ----------------------------------------------------------------

/**
 * Class IdleConnectionMonitorThread
 * Description
 * Create 2016-02-25 01:04:49 
 * @author Ardy    
 */
public class IdleConnectionMonitorThread extends Thread {

    /** 
     * Field logger
     * Description 
     */
    private final Logger logger = LoggerFactory.getLogger(IdleConnectionMonitorThread.class);

    /** 
     * Field idletime
     * Description 
     */
    private int idletime = 30;

    /** 
     * Field connMgr
     * Description 
     */
    private final HttpClientConnectionManager connMgr;

    /** 
     * Field shutdown
     * Description 
     */
    private volatile boolean shutdown;

    /**
     * Constructs IdleConnectionMonitorThread
     * Description
     *
     * @param connMgr 类型：HttpClientConnectionManager
     * @param idletime 类型：int
     */
    public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr, int idletime) {
        this.connMgr  = connMgr;
        this.idletime = idletime;
    }

    /**
     * Method setIdletime 
     * Description 说明：
     *
     * @param idletime 说明：
     */
    public void setIdletime(int idletime) {
        this.idletime = idletime;
    }

    /**
     * Method run 
     * Description 说明：
     */
    public void run() {
        while (true) {
            try {
                if (!this.shutdown) {
                    synchronized (this) {
                        this.wait(5000L);
                        this.connMgr.closeExpiredConnections();
                        this.connMgr.closeIdleConnections((long) this.idletime, TimeUnit.SECONDS);

                        continue;
                    }
                }
            } catch (InterruptedException var4) {
                logger.error("{}",var4);
            }

            return;
        }
    }

    /**
     * Method shutdown 
     * Description 说明：
     */
    public void shutdown() {
        this.shutdown = true;

        synchronized (this) {
            this.notifyAll();
        }

        logger.info("Shutdown IdleConnectionMonitorThread completed");
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
